import os, sys
import time
from PyQt5.QtCore import QThread, pyqtSignal
from PyQt5.QtWidgets import QApplication,QWidget
from PyQt5 import uic

class MyWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.my_thread = None
        self.ui = None
        self.ui = uic.loadUi(os.path.join(os.path.dirname(os.path.abspath(__file__)), '../ui/test02.thread.ui'))

        self.ui.pushButton.clicked.connect(self.button_click)

    def button_click(self):
        self.my_thread = MyThread()  # 创建线程
        self.my_thread.result_signal.connect(self.up_linetext)
        self.my_thread.percent_signal.connect(self.up_bar)
        self.my_thread.start()  # 开始线程

    def up_linetext(self,t):
        # text = self.ui.lineEdit.text() + t
        self.ui.lineEdit.setText(t)

    def up_bar(self,t):
        self.ui.progressBar.setValue(int(t))

class MyThread(QThread):
    result_signal = pyqtSignal(str)
    percent_signal = pyqtSignal(str)
    def __init__(self):
        super().__init__()

    def run(self):
        print("thread started")
        for i in range(101):
            time.sleep(0.1)
            self.percent_signal.emit(str(i))
        result = "操作完成"
        self.result_signal.emit(result)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = MyWindow()
    window.ui.show()
    app.exec()

